package com.nameless.controller.api;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nameless.annotation.HasPermission;
import com.nameless.entity.Role;
import com.nameless.service.RoleService;
import com.nameless.vo.Result;
import com.nameless.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 陈灰灰
 * @Date: 2023/02/27/10:51
 * @Description:
 */
@Api(value = "/system/api/role", tags = {"角色业务控制层"})
@RestController
@RequestMapping("/system/api/role")
public class ApiRoleController {
   @Resource
   private RoleService roleService;

    /**
     * @author chh
     * 角色列表
     * @param userid 用户标识
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userid", value = "用户标识", required = true)
    })
    @ApiOperation(value = "查询角色列表", notes = "角色列表")
    @RequestMapping("/roleList")
    public Result roleList(Integer userid){
    //查询所有的角色
    List<Role> list = roleService.list(null);
    //根据用户的id查询角色
    List<Integer> roleIds = roleService.findRolesByUserId(userid);
    System.out.println(roleIds);
    Map<String, Object> map = new HashMap<>();
    map.put("list",list);
    map.put("roleIds",roleIds);
    return new Result(200,"分配权限成功",map);
 }

    /**
     *  @author chh
     * qr确认分配角色
     * @param userid    用户标识
     * @param checkRole 检查作用
     * @return {@link Result}
     *///
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userid", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer[]", name = "checkRole", value = "", required = true)
    })
    @ApiOperation(value = "分配权限", notes = "分配权限")
    @RequestMapping("addUserAndRole")
    public Result qr(Integer userid,Integer[] checkRole){
    System.out.println(checkRole);
       return roleService.qr(userid,checkRole);
    }

    /**
     *  @author chh
     * 查询所有的角色
     * @param roleVo 签证官角色
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "RoleVo", name = "roleVo", value = "签证官角色", required = true)
    })
    @ApiOperation(value = "查询所有的角色", notes = "查询所有的角色")
    @RequestMapping("/queryAllByParam")
    @HasPermission("system:role:query")
    public Result queryAllByParam(RoleVo roleVo){
        Page<Role> rolePage = new Page<>(roleVo.getPageNum(),roleVo.getPageSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Role::getRoleId);
        queryWrapper.like(StringUtils.isNotEmpty(roleVo.getRoleName()),Role::getRoleName,roleVo.getRoleName());
        queryWrapper.like(StringUtils.isNotEmpty(roleVo.getCreateBy()),Role::getCreateBy,roleVo.getCreateBy());
        Page<Role> page = roleService.page(rolePage, queryWrapper);
        return new Result(200,"查询成功",page);
    }

    /**
     * @author chh
     * 更新作用
     * @param role 角色
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Role", name = "role", value = "角色", required = true)
    })
    @ApiOperation(value = "更新作用", notes = "更新作用")
    @RequestMapping("update")
    @HasPermission("system:role:edit")
    public Result updateRole(@RequestBody Role role){
        System.out.println(role);
        boolean b = roleService.updateById(role);
        return new Result(200,"修改成功");
    }

    /**
     * @Author chh
     * 添加角色
     * @param role 角色
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Role", name = "role", value = "角色", required = true)
    })
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @RequestMapping("insert")
    @HasPermission("system:role:add")
    public Result addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        return new Result(200,"添加成功");
    }

    /**
     * @Author
     * 删除
     * @param roleId 角色id
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "roleId", value = "角色id", required = true)
    })
    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping("delete")
    @HasPermission("system:role:remove")
    public Result delete(Integer roleId){
        boolean b = roleService.removeById(roleId);
        return new Result(200,"删除成功");

    }

}
